package org.framework.lazy.cloud.network.heartbeat.server.init;

import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ClientProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteContext;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.RouteType;
import org.framework.lazy.cloud.network.heartbeat.protocol.route.ServerProxyRoute;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.LazyNettyClientVirtualRouteApplication;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.LazyNettyServerVirtualRouteApplication;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.client.virtual.route.LazyNettyClientVirtualRouteQueryListCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.lazy.netty.server.virtual.route.LazyNettyServerVirtualRouteQueryListCommand;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyNettyClientVirtualRouteDTO;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyNettyServerVirtualRouteDTO;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;


@Slf4j
@Configuration
public class InitServerProxySocket implements CommandLineRunner {
    private final LazyNettyServerVirtualRouteApplication lazyNettyServerVirtualRouteApplication;
    private final LazyNettyClientVirtualRouteApplication lazyNettyClientVirtualRouteApplication;

    public InitServerProxySocket(LazyNettyServerVirtualRouteApplication lazyNettyServerVirtualRouteApplication, LazyNettyClientVirtualRouteApplication lazyNettyClientVirtualRouteApplication) {
        this.lazyNettyServerVirtualRouteApplication = lazyNettyServerVirtualRouteApplication;
        this.lazyNettyClientVirtualRouteApplication = lazyNettyClientVirtualRouteApplication;
    }

    @Override
    public void run(String... args) throws Exception {
        try {
            //初始化 服务端虚拟路由
            LazyNettyServerVirtualRouteQueryListCommand lazyNettyServerVirtualRouteQueryListCommand = new LazyNettyServerVirtualRouteQueryListCommand();
            lazyNettyServerVirtualRouteQueryListCommand.setIsDeleted(false);
            lazyNettyServerVirtualRouteApplication.findList(lazyNettyServerVirtualRouteQueryListCommand)
                    .accept(lazyNettyServerVirtualRouteDTOS -> {

                        for (LazyNettyServerVirtualRouteDTO lazyNettyServerVirtualRouteDTO : lazyNettyServerVirtualRouteDTOS) {
                            String virtualIp = lazyNettyServerVirtualRouteDTO.getVirtualIp();
                            String virtualPort = lazyNettyServerVirtualRouteDTO.getVirtualPort();
                            String targetIp = lazyNettyServerVirtualRouteDTO.getTargetIp();
                            String targetPort = lazyNettyServerVirtualRouteDTO.getTargetPort();

                            ServerProxyRoute serverProxyRoute = new ServerProxyRoute();
                            serverProxyRoute.setVirtualIp(virtualIp);
                            serverProxyRoute.setVirtualPort(virtualPort);
                            serverProxyRoute.setTargetIp(targetIp);
                            serverProxyRoute.setTargetPort(targetPort);
                            serverProxyRoute.setRouteType(RouteType.LOCAL);
                            RouteContext.setRoute(serverProxyRoute);
                        }

                    });
            // 初始化 客户端虚拟路由
            LazyNettyClientVirtualRouteQueryListCommand lazyNettyClientVirtualRouteQueryListCommand = new LazyNettyClientVirtualRouteQueryListCommand();
            lazyNettyClientVirtualRouteQueryListCommand.setIsDeleted(false);
            lazyNettyClientVirtualRouteApplication.findList(lazyNettyClientVirtualRouteQueryListCommand)
                    .accept(lazyNettyClientVirtualRouteDTOS -> {
                        for (LazyNettyClientVirtualRouteDTO lazyNettyClientVirtualRouteDTO : lazyNettyClientVirtualRouteDTOS) {
                            String virtualIp = lazyNettyClientVirtualRouteDTO.getVirtualIp();
                            String virtualPort = lazyNettyClientVirtualRouteDTO.getVirtualPort();
                            String targetIp = lazyNettyClientVirtualRouteDTO.getTargetIp();
                            String targetPort = lazyNettyClientVirtualRouteDTO.getTargetPort();
                            String targetClientId = lazyNettyClientVirtualRouteDTO.getTargetClientId();

                            ClientProxyRoute serverProxyClientRoute = new ClientProxyRoute();
                            serverProxyClientRoute.setVirtualIp(virtualIp);
                            serverProxyClientRoute.setVirtualPort(virtualPort);
                            serverProxyClientRoute.setTargetClientId(targetClientId);
                            serverProxyClientRoute.setTargetIp(targetIp);
                            serverProxyClientRoute.setTargetPort(targetPort);
                            serverProxyClientRoute.setRouteType(RouteType.SERVER_PROXY_CLIENT);
                            RouteContext.setRoute(serverProxyClientRoute);
                        }

                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
